package org.vison.wonfu.framework.config.utils;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PropertyConfigurer {

    private final static String defaultNamespace = "application";
    private static final Logger logger = LoggerFactory.getLogger(PropertyConfigurer.class);


    public static String getString(String key, String defaultValue) {
        String v =  ConfigService.getAppConfig().getProperty(key, defaultValue);
        logger.debug("get property key:{},value:{}",key,v);
        return v;
    }

    public static String getString(String namespace,String key, String defaultValue){
        if(namespace == null || namespace.isEmpty()){
            namespace = defaultNamespace;
        }
        return ConfigService.getConfig(namespace).getProperty(key, defaultValue);
    }

    public static Long getLong(String key, Long defaultValue) {
        return getLong(null,key,defaultValue);
    }

    public static Long getLong(String namespace,String key, Long defaultValue) {
        String value = getString(namespace,key, null);
        if(value == null) {
            return defaultValue;
        }
        return Long.parseLong(value);
    }

    public static Boolean getBoolean(String key, Boolean defaultValue) {
        String value = getString(key, null);
        if(value == null) {
            return defaultValue;
        }
        return "true".equalsIgnoreCase(value);
    }

     public static Boolean getBoolean(String namespace,String key, Boolean defaultValue) {
        String value = getString(namespace,key, null);
        if(value == null) {
            return defaultValue;
        }
        return "true".equalsIgnoreCase(value);
    }
    
    public static Integer getInteger(String key, Integer defaultValue) {
        return getInteger(null, key, defaultValue);
    }
    
    public static Integer getInteger(String namespace, String key, Integer defaultValue) {
        String value = getString(namespace,key, null);
        if(value == null) {
            return defaultValue;
        }
        return Integer.parseInt(value);
    }
    
    public static Double getDouble(String key, Double defaultValue) {
        return getDouble(null, key, defaultValue);
    }
    
    public static Double getDouble(String namespace, String key, Double defaultValue) {
        String value = getString(namespace,key, null);
        if(value == null) {
            return defaultValue;
        }
        return Double.parseDouble(value);
    }

}